package vis.core.common.util;


import java.util.*;

public class ArrayKit {
    //大于0返回true；否则返回false
    @SafeVarargs
    public static <T> ArrayList<T> asList(T... items) {
        ArrayList<T> objects = new ArrayList<>(items.length);
        objects.addAll(Arrays.asList(items));
        return objects;
    }


    /**
     * 切分数组
     *
     * @param collection 被切分的数组 [1,2,3,4,5]
     * @param size       长度
     * @param <R>        数组泛型
     * @return 切分后的数组 [[1,2],[3,4],[5]]
     */
    public static <R> ArrayList<ArrayList<R>> split(Collection<R> collection, int size) {
        ArrayList<R> list = new ArrayList<>(collection);
        if (list.size() == 0) return new ArrayList<>();
        ArrayList<ArrayList<R>> arrayLists = new ArrayList<>();
        arrayLists.add(new ArrayList<>(list));
        if (size == 0) return arrayLists;
        ArrayList<ArrayList<R>> splitList = new ArrayList<>();
        int cycleCount = list.size() / size;

        for (int i = 0; i < cycleCount; i++) {
            List<R> rs = list.subList(i * size, (i + 1) * size);
            splitList.add(new ArrayList<>(rs));
        }
        int yu = list.size() % size;
        if (yu > 0) {
            List<R> rs = list.subList(cycleCount * size, (cycleCount * size) + yu);
            splitList.add(new ArrayList<>(rs));
        }
        return splitList;
    }

    //大于0返回true；否则返回false
    public static boolean geZero(Collection<?> collection) {
        return collection != null && collection.size() > 0;
    }

    public static boolean isNotBlank(Collection<?> collection) {
        return collection != null && collection.size() != 0;
    }

    public static boolean isBlank(Collection<?> collection) {
        return !isNotBlank(collection);
    }

    public static boolean isNotBlank(Object... values) {
        return values != null && values.length != 0;
    }

    public static boolean isBlank(Object... values) {
        return !isNotBlank(values);
    }

    public static boolean contains(String content, List<String> contentList) {
        if (content == null || contentList == null || contentList.size() == 0) return false;

        for (String str : contentList) {
            if (content.contains(str)) {
                return true;
            }
        }
        return false;
    }

    public static String join(Collection<String> categoryName, String c) {
        return StringKit.join(categoryName, c);
    }

    /**
     * 从 list1 中，排除掉 list2 内存在的相同数据
     * 同时给  list1 和 list2 去重
     */
    public static ArrayList<String> excludeAndDeduplicate(ArrayList<String> list1, ArrayList<String> list2) {
        if (list1 == null || list1.size() == 0) return new ArrayList<>();
        if (list2 == null || list2.size() == 0) return list1;

        //allList：去重后的全部数据
        ArrayList<String> allList = new ArrayList<>(new HashSet<>(list1));

        //excludeList：去重后的待排除数据
        ArrayList<String> excludeList = new ArrayList<>(new HashSet<>(list2));

        ArrayList<String> newList = new ArrayList<>(allList.size());

        for (String nowStr : allList) {
            //标记   nowStr 是否在 excludeList集合中存在
            boolean isHas = false;

            for (String excludeStr : excludeList) {
                if (nowStr.equals(excludeStr)) {
                    isHas = true;
                    break;
                }
            }

            //如果遍历以后，nowStr 在excludeList集合中，不存在
            //此时将将其加入 newList中
            if (!isHas) {
                newList.add(nowStr);
            }
        }
        return newList;

    }

    /**
     * 返回分页后的数据
     *
     * @param collection 全部数据
     * @param pageNumber 想要查询的页数
     * @param pageSize   每页的大小
     */
    public static <R> ArrayList<R> toPage(Collection<R> collection, int pageNumber, int pageSize) {
        ArrayList<ArrayList<R>> split = split(collection, pageSize);
        if (split.size() == 0 || split.size() < pageNumber) return new ArrayList<>();
        return split.get(pageNumber - 1);
    }

//    public static void main(String[] args) {
//        List<Integer> integers = ArrayKit.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
//        System.out.println(split(integers, 1));
//        System.out.println(split(integers, 1).size());
//        System.out.println();
//        System.out.println(split(integers, 2));
//        System.out.println(split(integers, 2).size());
//        System.out.println();
//
//        System.out.println(split(integers, 3));
//        System.out.println(split(integers, 3).size());
//        System.out.println();
//
//        System.out.println(split(integers, 4));
//        System.out.println(split(integers, 4).size());
//        System.out.println();
//
//        System.out.println(split(integers, 5));
//        System.out.println(split(integers, 5).size());
//        System.out.println();
//
//        System.out.println(split(integers, 6));
//        System.out.println(split(integers, 6).size());
//        System.out.println();
//
//        System.out.println(split(integers, 7));
//        System.out.println(split(integers, 7).size());
//        System.out.println();
//
//        System.out.println(split(integers, 8));
//        System.out.println(split(integers, 8).size());
//        System.out.println();
//
//        System.out.println(split(integers, 9));
//        System.out.println(split(integers, 9).size());
//        System.out.println();
//
//        System.out.println(split(integers, 10));
//        System.out.println(split(integers, 10).size());
//        System.out.println();
//
//        System.out.println(split(integers, 11));
//        System.out.println(split(integers, 11).size());
//        System.out.println();
//
//        System.out.println(split(integers, 12));
//        System.out.println(split(integers, 12).size());
//        System.out.println();
//
//        System.out.println(split(integers, 13));
//        System.out.println(split(integers, 13).size());
//        System.out.println();
//
//    }
}
